package com.gopay.common.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.common.exception.GopayException;

/**
 * 
 * @Description: JSON操作工具类
 * @author hougn
 * @date 2016年4月25日 下午7:41:53 
 *
 */
public class GsrtJsonUtils {
    
    private static Logger logger = LoggerFactory.getLogger(GsrtJsonUtils.class);
	/**
     * 将Json转换为List
     * 
     * @param content
     * @return
     * @throws JsonParseException
     * @throws JsonMappingException
     * @throws IOException
     */
    @SuppressWarnings("unchecked")
    public static List<Map<String, Object>> toList(String content) throws GopayException {
        if (StringUtils.isBlank(content)) {
            return null;
        }

        List<Map<String, Object>> model = new ArrayList<Map<String, Object>>();
        ObjectMapper mapper = new ObjectMapper();

        try {
            model = mapper.readValue(content, List.class);
        } catch (IOException e) {
            logger.error("Json转换为List错误", e);
            throw new GopayException("", "Json转换为List错误");
        }

        return model;
    }
    /**
     * 将Json转换为Map
     * 
     * @param content
     * @return
     * @throws JsonParseException
     * @throws JsonMappingException
     * @throws IOException
     */
    @SuppressWarnings("unchecked")
    public static Map<String, String> toMap(String content) throws GopayException {
        if (StringUtils.isBlank(content)) {
            return null;
        }

        Map<String, String> model = new HashMap<String, String>();
        ObjectMapper mapper = new ObjectMapper();

        try {
            model = mapper.readValue(content, Map.class);
        } catch (IOException e) {
            logger.error("JSON串转为Map错误", e);
            throw new GopayException("", "JSON串转为Map错误");
        }

        return model;
    }

    /**
     * 将JSON串转为Map
     * 
     * @param json
     * @return
     * @throws GopayException
     */
    @SuppressWarnings("unchecked")
    public static Map<String, Object> jsonToMap(String json) throws GopayException {
        if (StringUtils.isBlank(json)) {
            return null;
        }

        Map<String, Object> model = new HashMap<String, Object>();
        ObjectMapper mapper = new ObjectMapper();

        try {
            model = mapper.readValue(json, Map.class);
        } catch (IOException e) {
            logger.error("JSON串转为Map错误", e);
            throw new GopayException("", "JSON串转为Map错误");
        }

        return model;
    }

    /**
     * 将对象解析为json串
     * 
     * @param obj
     * @return
     * @throws GopayException 
     */
    public static String beantoJson(Object obj) throws GopayException {
        ObjectMapper mapper = new ObjectMapper();
        String json = null;
        try {
            json = mapper.writeValueAsString(obj);
            json = json.replaceAll("\\\\", "");
            json = json.replaceAll("\"\\{", "\\{");
            json = json.replaceAll("\\}\"", "\\}");
        } catch (IOException e) {
            logger.error("对象解析为json串错误", e);
            throw new GopayException("", "对象解析为json串错误");
        } 

        return json;
    }
    
    /**
     * json转对象
     * @param json
     * @param valueType
     * @return
     * @throws GopayException
     */
    public static <T> T jsontoBean(String json, Class<T> valueType) throws GopayException {
        ObjectMapper mapper = new ObjectMapper();
        
        try {
            return mapper.readValue(json, valueType);
        } catch (IOException e) {
            logger.error("对象解析为json串错误", e);
            throw new GopayException("", "对象解析为json串错误");
        }
    }
}
